package org.tgcloud.group.web.controller.manager;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.core.custom.Permissions;
import org.tgcloud.group.core.session.Session;
import org.tgcloud.group.service.manager.ProjectManageService;
import org.tgcloud.group.web.controller.dto.order.ProjectListDTO;
import org.tgcloud.group.web.controller.req.project.ProjectEditReq;
import org.tgcloud.group.web.controller.req.project.ProjectIdReq;
import org.tgcloud.group.web.controller.req.project.ProjectPageReq;
import org.tgcloud.group.web.controller.req.project.intendedMedia.ProjectAddIntendedMediaReq;
import org.tgcloud.group.web.controller.req.project.mediaRequirements.MediaRequirementsBatchCreateReq;
import org.tgcloud.group.web.controller.req.project.mediaRequirements.MediaRequirementsCreateReq;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author: zhy
 * @create: 2023-08-18 10:01
 * @description: 项目相关接口
 */
@Api(tags = "项目管理相关接口")
@RestController
@RequestMapping("/manager/pc/user")
@EnableAutoConfiguration
public class ProjectManageController extends ManageAPPBaseController{

    @Autowired
    private ProjectManageService projectManageService;

    @Permissions("project:view")
    @ApiOperation(value = "项目列表")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/getProjectManageList")
    public APPResultMessage<Object, ProjectListDTO> getProjectManageList(@RequestBody ProjectPageReq req,
                                                                         @ModelAttribute("session")Session session) {
        return projectManageService.getProjectManageList(req, session.getUserId(), session.getComId());
    }


    @Permissions("project:edit,project:editGoodArrival,project:editInvoice,project:editActualValuePrice")
    @ApiOperation(value = "项目编辑")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/editManageProject")
    public APPResultMessage<Object, List> editManageProject(@RequestBody ProjectEditReq req,
                                                            @ModelAttribute("session")Session session) {
        return projectManageService.editManageProject(req,session.getUserId(), session.getUserType());
    }


//    @Permissions("project:add")
//    @ApiOperation(value = "项目新增")
//    @ApiImplicitParams({
//    })
//    @ApiResponses({
//            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
//    })
//    @ResponseBody
//    @PostMapping(value = "/addManageProject")
//    public APPResultMessage<Object, List> addManageProject(@RequestBody ProjectEditReq req,
//                                                           @ModelAttribute("session")Session session) {
//        return projectManageService.addManageProject(req, session.getUserId(), session.getUserType());
//    }

    @Permissions("project:add")
    @ApiOperation(value = "项目新增(new)")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/addProjectOnly")
    public APPResultMessage<Object, List> addManageProject(@RequestBody ProjectEditReq req,
                                                           @ModelAttribute("session")Session session) {
        return projectManageService.addOnlyProject(req, session.getUserId(), session.getUserType());
    }

    @Permissions("project:add")
    @ApiOperation(value = "媒体需求新增(new)")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/addMediaRequirementsOnly")
    public APPResultMessage<Object, List> addManageProject(@RequestBody MediaRequirementsBatchCreateReq req,
                                                           @ModelAttribute("session")Session session) {
        return projectManageService.addMediaRequirementsOnly(req);
    }

//    @Permissions("project:add")
//    @ApiOperation(value = "媒体需要(new)")
//    @ApiImplicitParams({
//    })
//    @ApiResponses({
//            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
//    })
//    @ResponseBody
//    @PostMapping(value = "/addMediaRequirementsOnly")
//    public APPResultMessage<Object, List> addManageProject(@RequestBody MediaRequirementsCreateReq req,
//                                                           @ModelAttribute("session")Session session) {
//        return projectManageService.add(req);
//    }





    @Permissions("project:del")
    @ApiOperation(value = "项目删除")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/delManageProject")
    public APPResultMessage<Object, List> delManageProject(@RequestBody ProjectIdReq req,
                                                           @ModelAttribute("session")Session session) {
        return projectManageService.delManageProject(req,session.getUserId(), session.getUserType());
    }



    @ApiOperation(value = "项目详情")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/getProjectManageMsg")
    public APPResultMessage<ProjectListDTO, List> getProjectManageMsg(@RequestBody ProjectIdReq req) {
        return projectManageService.getProjectManageMsg(req);
    }


    @Permissions("project:editContract")
    @ApiOperation(value = "项目上传合同文件")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/editManageProjectContract")
    public APPResultMessage editManageProjectContract(@RequestBody ProjectEditReq req) {
        return projectManageService.editManageProjectContract(req);
    }


    @Permissions("project:editExecutionProgress,project:editArrivalProgress,project:editArrivalWarehouse")
    @ApiOperation(value = "项目修改进度")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/editManageArrivalProgress")
    public APPResultMessage editManageArrivalProgress(@RequestBody ProjectEditReq req) {
        return projectManageService.editManageArrivalProgress(req);
    }



    @Permissions("project:editActualValuePrice")
    @ApiOperation(value = "项目修改实际货值")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/editManageActualValuePrice")
    public APPResultMessage editManageActualValuePrice(@RequestBody ProjectEditReq req) {
        return projectManageService.editManageActualValuePrice(req);
    }


    @Permissions("project:editProjectCheckTime")
    @ApiOperation(value = "项目修改签单时间")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/editProjectCheckTime")
    public APPResultMessage editProjectCheckTime(@RequestParam int projectId,
                                                 @RequestParam(required = false) Long targetCheckTime,
                                                 @ModelAttribute("session") Session session) {
        return projectManageService.editProjectCheckTime(projectId, targetCheckTime == null ? new Date() : new Date(targetCheckTime),session.getUserId());
    }


    @Permissions("project:editDiscount")
    @ApiOperation(value = "项目设置是否打折")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/editManageDiscount")
    public APPResultMessage editManageDiscount(@RequestBody ProjectEditReq req) {
        return projectManageService.editManageDiscount(req);
    }



//    @Permissions("project:sureCheckProject")
//    @ApiOperation(value = "确认签单")
//    @ApiImplicitParams({
//    })
//    @ApiResponses({
//            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
//    })
//    @ResponseBody
//    @PostMapping(value = "/doSureCheckProject")
//    public APPResultMessage doSureCheckProject(@RequestParam int projectId,
//                                               @RequestParam(required = false) Long chooseDate,
//                                               @ModelAttribute("session") Session session) {
//        return projectManageService.doSureCheckProject(projectId, chooseDate == null ? new Date() : new Date(chooseDate), session.getUserId());
//    }

    @Permissions("project:sureCheckProject")
    @ApiOperation(value = "确认合同终版并签单")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/doSureCheckProjectAndSure")
    public APPResultMessage doSureCheckProjectAndSure(@RequestParam int projectId,
                                                      @RequestParam(required = false) Long chooseDate,
                                                      @RequestParam(required = false) Integer fileId,
                                                      @RequestParam(required = false) String productName,
                                                      @RequestParam(required = false) String invoiceCompany,
                                                      @RequestParam(required = false) BigDecimal invoicePrice,
                                                      @RequestParam(required = false) String orderNo,
                                                      @ModelAttribute("session") Session session) {
        return projectManageService.doSureCheckProjectAndSure(projectId, chooseDate == null ? new Date() : new Date(chooseDate), session.getUserId(),fileId,session.getUserType(),productName,invoiceCompany,invoicePrice,orderNo);
    }

//    @Permissions("project:sureCheckProject")
    @ApiOperation(value = "确认配包终版")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/doSurePackage")
    public APPResultMessage doSurePackage(@RequestParam int projectId,
                                                      @RequestParam(required = false) Integer fileId,
                                                      @RequestParam(required = false) BigDecimal estimatedSelfMediaCost,
                                                      @RequestParam(required = false) BigDecimal estimatedExternalMediaCost,
                                                      @ModelAttribute("session") Session session) {
        return projectManageService.doSurePackage(projectId,  session.getUserId(),fileId,session.getUserType(),estimatedSelfMediaCost,estimatedExternalMediaCost);
    }







    @Permissions("project:fail")
    @ApiOperation(value = "项目已黄单")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/doFailManageProject")
    public APPResultMessage doFailManageProject(@RequestParam int projectId,
                                                @RequestParam String failReason) {
        return projectManageService.doFailManageProject(projectId, failReason);
    }



    @ApiOperation(value = "项目配包")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/doPackageManageProject")
    public APPResultMessage doPackageManageProject(@RequestParam int projectId,
                                                   @RequestParam String mediums,
                                                   @ModelAttribute("session") Session session,
                                                   @RequestParam(required = false) Integer chooseAddIndex) {
        if (StringUtil.isBlank(mediums)){
            return new APPResultMessage(6001, "请选择媒体信息");
        }
        return projectManageService.doPackageManageProject(session.getUserId(), projectId, mediums, chooseAddIndex);
    }



    @ApiOperation(value = "项目查看配包列表")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/viewPackageManageProjectList")
    public APPResultMessage viewPackageManageProjectList(@RequestParam int projectId) {
        return projectManageService.viewPackageManageProjectList(projectId);
    }


    @ApiOperation(value = "项目查看配包详情")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/viewPackageManageProjectMsg")
    public APPResultMessage viewPackageManageProjectMsg(@RequestParam int projectId,
                                                        @RequestParam int indexNum) {
        return projectManageService.viewPackageManageProjectMsg(projectId,indexNum);
    }


    @ApiOperation(value = "查询时间段内所有的已配包的项目")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/viewOfflineHasPackageProjectList")
    public APPResultMessage viewOfflineHasPackageProjectList(@RequestParam(required = false) Long startTime,
                                                             @RequestParam(required = false) Long endTime,
                                                             @RequestParam(required = false) Integer projectId,
                                                             @RequestParam(required = false) Integer chooseProjectId,
                                                             @RequestParam(required = false) Integer addIndex,
                                                             @RequestParam(required = false) String chooseYihuoProjectIds,
                                                             @RequestParam(required = false) String chooseMediumOtherProjectIds,
                                                             @RequestParam(required = false) Integer showChooseProjectType,
                                                             @ModelAttribute("session") Session session) {
        return projectManageService.viewOfflineHasPackageProjectList(startTime == null ? null : new Date(startTime),
                endTime == null? null : new Date(endTime), projectId, addIndex, chooseProjectId, chooseYihuoProjectIds, showChooseProjectType,
                chooseMediumOtherProjectIds, session.getComId());
    }




    @ApiOperation(value = "删除配包")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/doDelProjectPackage")
    public APPResultMessage doDelProjectPackage(@RequestParam int projectId,
                                                        @RequestParam int indexNum,
                                                @ModelAttribute("session") Session session) {
        return projectManageService.doDelProjectPackage(projectId,indexNum, session.getUserId());
    }


    @ApiOperation(value = "设置配包时间")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/doSetProjectPackageTime")
    public APPResultMessage doSetProjectPackageTime(@RequestParam int projectId,
                                                    @RequestParam int lineId,
                                                    @RequestParam(required = false) Integer chooseId,
                                                    @RequestParam Long chooseTime,
                                                    @RequestParam(required = false) Integer addIndex,
                                                    @ModelAttribute("session") Session session,
                                                    @RequestParam(required = false) Integer endDayNum,
                                                    @RequestParam(required = false) Integer lineIndex,
                                                    @RequestParam(required = false) Integer areaType) {
        return projectManageService.doSetProjectPackageTime(projectId,lineId,chooseId,
                chooseTime == null ? null : new Date(chooseTime), addIndex, session.getUserId(), endDayNum, lineIndex, areaType);
    }



    @ApiOperation(value = "确认配包")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/doSureProjectPackage")
    public APPResultMessage doSureProjectPackage(@RequestParam int projectId,
                                                @RequestParam int indexNum) {
        return projectManageService.doSureProjectPackage(projectId,indexNum);
    }


    @ApiOperation(value = "查询项目名称是否重复")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/doGetHasAddProjectName")
    public APPResultMessage doGetHasAddProjectName(@RequestParam String name,
                                                   @RequestParam(required = false) Integer projectId) {
        if (StringUtil.isBlank(name)){
            return new APPResultMessage(6001, "请输入项目名称");
        }
        return projectManageService.doGetHasAddProjectName(name, projectId);
    }



    @Permissions("project:sureFinalProject")
    @ApiOperation(value = "执行完项目")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/doSureFinalProject")
    public APPResultMessage doSureFinalProject(@RequestParam int projectId,
                                               @RequestParam(required = false) Long chooseDate) {
        return projectManageService.doSureFinalProject(projectId, chooseDate == null ? new Date() : new Date(chooseDate));
    }


    @ApiOperation(value = "查看单独项目的配包")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/getOneProjectPackageList")
    public APPResultMessage getOneProjectPackageList(@RequestParam int projectId,
                                                     @RequestParam Short lineType) {
        return projectManageService.getOneProjectPackageList(projectId, lineType);
    }



    @Permissions("project:addIntendedMedia")
    @ApiOperation(value = "项目选择报备媒体")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/doAddIntendedMedia")
    public APPResultMessage doAddIntendedMedia(@RequestBody ProjectAddIntendedMediaReq req) {
        return projectManageService.doAddIntendedMedia(req);
    }

    @ApiOperation(value = "开始执行")
    @Permissions("project:startExecute")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/startExecute")
    public APPResultMessage startExecute(@RequestParam int projectId,
                                         @ModelAttribute("session") Session session) {
        return projectManageService.startExecute(projectId);
    }

    @ApiOperation(value = "执行完成")
    @ApiImplicitParams({
    })
    @Permissions("project:completeExecute")

    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/completeExecute")
    public APPResultMessage completeExecute(@RequestParam int projectId,
                                         @RequestParam BigDecimal actualCost,
                                         @RequestParam Long executeTime,
                                         @ModelAttribute("session") Session session) {
        return projectManageService.completeExecute(projectId,actualCost, executeTime != null ? new Date(executeTime) : null);
    }

    @ApiOperation(value = "获取执行完成信息")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/getCompleteExecute")
    public APPResultMessage getCompleteExecute(@RequestParam int projectId,
                                            @ModelAttribute("session") Session session) {
        return projectManageService.getCompleteExecute(projectId);
    }



}
